define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Group, $window, $timeout, $ionicSlideBoxDelegate, $ionicPopover, GoPage, GoDialog, $stateParams,GoAddress,$ionicModal,GoPicture) {
		$scope.act_id = $stateParams.id;
		$scope.scope = new Object();
		$scope.cat = new Array();
		var cat_type = new Array();
		$scope.cat_types = new Array();
		$scope.store = new Array();
		$scope.type = 0;
		var store = new Array();
		var show_store = new Array();
		var region = new Array();
		var goods = new Array();
		$scope.ad_region = new Array();
		var rule = new Array();
		var x = 0;
		$scope.scope.is_change = false;
		$scope.scope.image = new Array();
		$scope.scope.create_group = false;
		$scope.scope.has_packets = false;
		$scope.scope.cat_type = new Array();
		$scope.datas = new Object();
		$scope.input = new Object();
		$scope.input.key = null;
		$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
		$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
		$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.input.region = null;
		$scope.input.cat_type = null;
		$scope.input.agent_count = 0;
		$scope.input.goods_count = 0;
		$scope.is_same = false;
		$scope.goods_is_same = false;
		$scope.province_name = null;
		$scope.city_name = null;
		$scope.districe_name = null;
		$scope.region_name = null;
		$scope.cat_id = null;
		$scope.cat_name = null;
		$scope.input.name = null;
		$scope.input.brand_id = null;
		$scope.input.cat1 = { id: 0, name: "请选择大分类" };
		$scope.input.cat2 = { id: 0, name: "请选择中分类" };
		$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		$scope.isLoad = false; //可以加载
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
		};
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}, 1000);
		};
		//图片移除
		$scope.ondelete = function(index) {
			$scope.scope.image.splice(index, 1);
		};
		//动态响应
		$scope.doShowFile = function() {
				document.getElementById("upfilebutton").click();
			}
			//图片上传
		$scope.onupload = function(val) {
			var fileList = val.files;
			for(var i = 0; i < val.files.length; i++) {
				var reader = new FileReader();
				reader.readAsDataURL(fileList[i]);
				var file = fileList[i];
				reader.onload = function(e) {
					//图片进行压缩处理
					GoPicture.compress(file,e.target.result, 50, function(base64) {
						$scope.$apply(function() {
							var i = $scope.scope.image.length;
							$scope.scope.image[i] = {
								"id": null,
								"image": base64
							}
							val.value = "";
						});
					});
				}
			}
			val.value="";
		};
		//点击团购，显示团购的详情
		$scope.detailContent = new Array();
		$scope.init_detailContent = function(goods) {
			for(var i = 0; i < goods.length; i++) {
				var n = $scope.detailContent.length;
				var id = goods[i].goods_id;
				$scope.detailContent[id] = {
					"checked": false,
					"id": id
				}
			}
		}
		$scope.detailChanged = function(index) {
			for(var o in $scope.detailContent) {
				if($scope.detailContent[o].id == index) {
					if($scope.detailContent[o].checked == true)
						$scope.detailContent[o].checked = false;
					else
						$scope.detailContent[o].checked = true;
				} else
					$scope.detailContent[o].checked = false;
			}

		}
		$scope.add_type = function() {
			if($scope.scope.cat1 !=undefined&&$scope.scope.cat1.id !=0) {
				$scope.cat_id = $scope.scope.id1;
				//等有了品类联动接口再替换成name
				$scope.cat_name = $scope.scope.id1;
				if($scope.scope.id2 !=undefined&&$scope.scope.cat2.id !=0) {
					$scope.cat_id = $scope.scope.id1 + ',' + $scope.scope.id2;
					$scope.cat_name = $scope.scope.id1 + '-' + $scope.scope.id2;
					if($scope.scope.id3 !=undefined&&$scope.scope.cat3.id !=0) {
						$scope.cat_id = $scope.scope.id1 + ',' + $scope.scope.id2 + ',' + $scope.scope.id3;
						$scope.cat_name = $scope.scope.id1 + '-' + $scope.scope.id2 + '-' + $scope.scope.id3;
					}
				}
			}
			var n = $scope.scope.cat_type.length;
			for(var i = 0; i < n; i++) {
				if($scope.scope.cat_type[i].id == $scope.cat_id)
					return;
			}
			if($scope.cat_id != null)
				$scope.scope.cat_type[n] = {
					"id": $scope.cat_id,
					"name": $scope.cat_name
				};
		}
		$scope.delete_type = function(index) {
			$scope.scope.cat_type.splice(index, 1);
		}
		$scope.choose_store = function(id) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == id) {
					$scope.region = $scope.datas.store[i].region;
				}
			}
		}
		$scope.get_store = function() {
			store = new Array();
			if($scope.datas.store == undefined) {
				GoDialog.toast("请选择活动代理商");
			} else if($scope.datas.store.length<1){
				GoDialog.toast("请选择活动代理商");
			}
			else {
				for(var i = 0; i < $scope.datas.store.length; i++) {
					if($scope.datas.store[i].region == undefined) {
						GoDialog.toast("请选择" + $scope.datas.store[i].store_name + "的活动地域");
						return;
					} else if($scope.datas.store[i].region.length<1){
						GoDialog.toast("请选择" + $scope.datas.store[i].store_name + "的活动地域");
						return;
					}
					else {
						for(var n = 0; n < $scope.datas.store[i].region.length; n++) {
							region[n] = {
								"region_id": $scope.datas.store[i].region[n].region_id
							};
						}
					}
					store[i] = {
						"store_id":$scope.datas.store[i].store_id,
						"change_price":$scope.datas.store[i].change_price,
						"region":region
					}
				}
				Group.group_createedit($scope.id, $scope.scope.name, $scope.scope.start_time, $scope.scope.end_time, 'group', 'packets', $scope.scope.image, $scope.scope.brand_id, $scope.cat, store, goods, function(data) {
				if(data.success == true) {
					GoDialog.toast("保存成功")
				} else
					GoDialog.toast("保存失败")
				goods = new Array();

			});
			}
			

		}
		$scope.delete_region = function(id) {
			$scope.ad_region.splice(id, 1);
			region.splice(id, 1);
			for(var i = 0; i < $scope.ad_region.length; i++) {
				$scope.ad_region[i].id = i;
			}
		}
		$scope.get_goods = function() {
			goods = new Array();
			if($scope.datas.goods == undefined) {
				GoDialog.toast("请选择活动商品");
			} else {
				for(var m = 0; m < $scope.datas.goods.length; m++) {
					if($scope.datas.goods[m].deposit == undefined | $scope.datas.goods[m].deposit == 0 | $scope.datas.goods[m].deposit == null) {
						GoDialog.toast("请输入" + $scope.datas.goods[m].goods_name + "的定金");
						return;
					}
					if($scope.datas.goods[m].count == undefined|$scope.datas.goods[m].count == 0 | $scope.datas.goods[m].count == null  ) {
						GoDialog.toast("请输入" + $scope.datas.goods[m].goods_name + "的参加活动件数");
						return;
					}
					if($scope.datas.goods[m].least_buy_num == undefined|$scope.datas.goods[m].least_buy_num == 0 | $scope.datas.goods[m].least_buy_num == null  ) {
						GoDialog.toast("请输入" + $scope.datas.goods[m].goods_name + "的起购数");
						return;
					}
					if($scope.datas.goods[m].groupbuy_rule == undefined)
						$scope.datas.goods[m].groupbuy_rule = new Array();
					if($scope.datas.goods[m].groupbuy_rule.length < 1) {
						GoDialog.toast("请设定" + $scope.datas.goods[m].goods_name + "的活动规则");
						return;
					}
					var i = goods.length;
					goods[i] = $scope.datas.goods[m];
				}
				$scope.get_store();
			}

		}
		$scope.rule_choose = function(type) {
			if(type == 1) {
				$scope.type = 1;
			} else if(type == 2) {
				$scope.type = 2;
			} else if(type == 3) {
				$scope.type = 3;
			}
		}
		$scope.get_rule = function() {
			rule = new Array();
			if($scope.type == 0) {
				GoDialog.toast("请选择红包类型");
			} else {
				if($scope.scope.type1 == true) {
					x = x + 1;
					if(!$scope.scope.count1) {
						GoDialog.toast("请输入商品数量");
						return;
					} else
						$scope.count = $scope.scope.count1;
					if(!$scope.scope.money1) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money1;
					if(!$scope.scope.packet_num1) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num1;
					if(!$scope.scope.issue_num1) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num1;
					$scope.rule_data(1);

				}
				if($scope.scope.type2 == true) {
					x = x + 1;
					if(!$scope.scope.count2) {
						GoDialog.toast("请输入订单数量");
						return;
					} else
						$scope.count = $scope.scope.count2;
					if(!$scope.scope.money2) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money2;
					if(!$scope.scope.packet_num2) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num2;
					if(!$scope.scope.issue_num2) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num2;
					$scope.rule_data(2);
				}
				if($scope.scope.type3 == true) {
					x = x + 1;
					if(!$scope.scope.count3) {
						GoDialog.toast("请输入成交额");
						return;
					} else
						$scope.count = $scope.scope.count3;
					if(!$scope.scope.money3) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money3;
					if(!$scope.scope.packet_num3) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num3;
					if(!$scope.scope.issue_num3) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num3;
					$scope.rule_data(3);
				}
			}
		}
		$scope.rule_data = function(type) {
			var n = rule.length;
			rule[n] = {
				"type": type,
				"count": $scope.count,
				"packet_num": $scope.packet_num,
				"issue_num": $scope.issue_num
			};
		}
		$scope.packets_edit = function() {
			$scope.get_rule();
			if(rule.length == x && x != 0) {
				Group.packets_edit($scope.id, $scope.scope.flg, $scope.scope.advance, rule, function(data) {
					if(data.success == true) {
						GoDialog.toast("保存成功");

					} else
						GoDialog.toast("保存失败");
				})
			}
			rule = new Array();
			x = 0;
		}
		$scope.add_goods_rule = function(id) {
			for(var i = 0; i < $scope.datas.goods.length; i++) {
				if($scope.datas.goods[i].goods_id == id) {
					var n = $scope.datas.goods[i].groupbuy_rule.length;
					if($scope.datas.goods[i].groupbuy_rule.length < 1) {
						var j = $scope.datas.goods[i].groupbuy_rule.length;
						$scope.datas.goods[i].groupbuy_rule[j] = {
							"id": null,
							"group_count": null,
							"days": null,
							"promotion_price": null
						}
					} else
					if($scope.datas.goods[i].groupbuy_rule[n - 1].group_count && $scope.datas.goods[i].groupbuy_rule[n - 1].days && $scope.datas.goods[i].groupbuy_rule[n - 1].promotion_price)
						$scope.datas.goods[i].groupbuy_rule[n] = {
							"id": null,
							"group_count": null,
							"days": null,
							"promotion_price": null
						}

					break;
				}
			}
		}
		$scope.save_goods_rule = function() {
			for(var i = 0; i < $scope.datas.goods.length; i++) {
				var n = $scope.datas.goods[i].groupbuy_rule.length;
				if(n > 1) {
					if(!$scope.datas.goods[i].groupbuy_rule[n - 1].group_count || !$scope.datas.goods[i].groupbuy_rule[n - 1].days || !$scope.datas.goods[i].groupbuy_rule[n - 1].promotion_price)
						$scope.datas.goods[i].groupbuy_rule.splice(n - 1, 1);
				}
			}
		}
		$scope.submit = function() {

			if($scope.scope.name == undefined) {
				GoDialog.toast("请填写活动名称");
				return;
			}
			if($scope.scope.start_time == undefined) {
				GoDialog.toast("请填写活动开始时间");
				return;
			}
			if($scope.scope.end_time == undefined) {
				GoDialog.toast("请填写活动结束时间");
				return;
			}
			if($scope.scope.cat_type.length < 1) {
				GoDialog.toast("请选择活动的品类");
				return;
			}
			$scope.get_goods();
			//			if(goods.length<1){
			//				GoDialog.toast("请选择活动商品");
			//				$scope.get_goods();
			//					return;
			//			}
			//			$scope.get_store();
			//			if(store.length<1){
			//					GoDialog.toast("请选择活动代理商");
			//					$scope.get_store();
			//					return;
			//			}
			//			$scope.get_goods();
			//			$scope.get_store();

		}
		$scope.init_data = function() {
				$scope.bind('input.province', 'input.city', 'input.district');
				if($scope.act_id != null) {
					Group.agenter_groupbuy($scope.act_id, function(data) {
						$scope.datas = data;
						$scope.init_detailContent($scope.datas.goods);
						for(var i = 0; i < $scope.datas.store.length; i++) {
							$scope.datas.store[i].province = null;
							$scope.datas.store[i].city = null;
							$scope.datas.store[i].district = null;
							//初始化想要绑定的模型
							$scope.bind('datas.store[' + i + '].province', 'datas.store[' + i + '].city', 'datas.store[' + i + '].district', i);
						}
					});
					Group.groupbuy_detail($scope.act_id, function(data) {
						$scope.scope = data;
					})
				}

			}
			//独立每个城市跟城区的itmes
		$scope.listen_province = function(data, id) {
			//动态刷新items，并且防止删除时的回调报错
			if(id != undefined&&$scope.datas.store.length>id) {
				$scope.datas.store[id].city_items = data;
				$scope.datas.store[id].district_items = null;
				$scope.datas.store[id].city = null;
				$scope.datas.store[id].district = null;
			} else {
				$scope.input.city = null;
				$scope.input.district = null;
				$scope.city_items = data;
				$scope.district_items = null;
			}

		};
		$scope.listen_city = function(data, id) {
		if(id != undefined)
				$scope.datas.store[id].district_items = data;
			else
				$scope.input.district = null;
		};
		//动态绑定三级地域的模型
		$scope.bind = function(ob1, ob2, ob3, i) {
			var items = new Array();
			var obj1 = {
				name: ob1,
				id: i
			};
			var obj2 = {
				name: ob2,
				id: i
			};
			var obj3 = {
				name: ob3,
				id: i
			};
			items.push(obj1);
			items.push(obj2);
			items.push(obj3);
			GoAddress.init_load($scope, items);
		}
		$scope.doSelectBrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择品牌",$scope.brand_items,$scope.input.brand,function(data){
				$scope.input.brand_id = data.id;
				$scope.input.brand = data;
			});
		}
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
		$scope.doSelectProvince = function(store){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.store.province,function(data){
				store.province = data;
			});
		}
		$scope.doSelectCity = function(store){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.store.city,function(data){
				store.city = data;
			});
		}
		$scope.doSelectDistrict = function(store){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.store.district,function(data){
				store.district = data;
			});
		}
		$scope.region_choose = function(store_id) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					if($scope.datas.store[i].province != null) {
						if($scope.datas.store[i].region == undefined)
							$scope.datas.store[i].region = new Array();
						var n = $scope.datas.store[i].region.length;
						$scope.region_name = $scope.datas.store[i].province.name;
						$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].province.id;
						if($scope.datas.store[i].city != null) {
							$scope.region_name += '/' + $scope.datas.store[i].city.name;
							$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].city.id;
						}
						if($scope.datas.store[i].district != null) {
							$scope.region_name += '/' + $scope.datas.store[i].district.name;
							$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].district.id;
						}
						for(var n = 0; n < $scope.datas.store[i].region.length; n++) {
							if($scope.datas.store[i].region[n].region_id == $scope.region_id) {
								GoDialog.toast("该地域已存在");
								return;
							}

						}
						$scope.datas.store[i].region[n] = {
							"region_id": $scope.region_id,
							"region_name": $scope.region_name

						}
						return;
					}

				}
			}
		}
		$scope.region_delete = function(store_id, index) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					$scope.datas.store[i].region.splice(index, 1);
					return;
				}
			}
		}
		$scope.delete_store = function(store_id) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					$scope.datas.store[i].checked = false;
					$scope.datas.store.splice(i, 1);
				}
			}
		}
		$ionicModal.fromTemplateUrl('templates/modal/searchpro_agent.html', {
			scope: $scope,
		}).then(function(modal) {
			$scope.specModal = modal;
		});
		$scope.openSpec = function() {
			$scope.input.province = null; //省市区三级联动
			$scope.input.city = null;
			$scope.input.district = null;
			$scope.input.province_name = null; //省市区三级联动
			$scope.input.city_name = null;
			$scope.input.district_name = null;
			$scope.input.agent_count = 0;
			$scope.agenter_list = new Object();
			$scope.specModal.show();
		};
		$scope.closeSpec = function() {
			if($scope.datas.store == undefined)
				$scope.datas.store = new Array();
			if($scope.agenter_list != undefined) {
				for(var i = 0; i < $scope.agenter_list.store.length; i++) {
					if($scope.agenter_list.store[i].checked == true) {
						$scope.is_same = false;
						for(var n = 0; n < $scope.datas.store.length; n++) {
							if($scope.datas.store[n].store_id == $scope.agenter_list.store[i].store_id) {
								$scope.is_same = true;
								break;
							}

						};
						if($scope.is_same == false) {
							var n = $scope.datas.store.length;
							$scope.datas.store[n] = {
								"store_id": $scope.agenter_list.store[i].store_id,
								"store_name": $scope.agenter_list.store[i].store_name,
								"change_price": false,
								"province":null,
								"city":null,
								"district":null,

							};

							$scope.bind('datas.store['+n+'].province','datas.store['+n+'].city','datas.store['+n+'].district',n);
						}

					}
				}
			}
			$scope.specModal.hide();
		};
		$ionicModal.fromTemplateUrl('templates/modal/searchpro_common.html', {
			scope: $scope,
			//			animation: 'slide-in-up'
		}).then(function(modal) {
			$scope.goodsModal = modal;
		});
		$scope.openGoods = function() {
			$scope.goodsModal.show();
		};
		$scope.closeGoods = function() {
			if($scope.datas.goods == undefined)
				$scope.datas.goods = new Array();
			if($scope.goods_data != undefined) {
				for(var n = 0; n < $scope.goods_data.goods.length; n++) {
					if($scope.goods_data.goods[n].checked == true) {
						$scope.goods_is_same = false;
						for(var i = 0; i < $scope.datas.goods.length; i++) {
							if($scope.goods_data.goods[n].goods_id == $scope.datas.goods[i].goods_id) {
								$scope.goods_is_same = true;
								break;
							}

						}
						if($scope.goods_is_same == false) {
							var m = $scope.datas.goods.length;
							$scope.datas.goods[m] = {
								"goods_id": $scope.goods_data.goods[n].goods_id,
								"goods_name": $scope.goods_data.goods[n].name,
								"promotion_price": $scope.goods_data.goods[n].preprice,
								"count": $scope.goods_data.goods[n].store,
								"image": $scope.goods_data.goods[n].image.image,
								"least_buy_num": 1,
								"price": $scope.goods_data.goods[n].price,
								"groupbuy_rule": new Array(),
								"deposit": null
							};
							$scope.init_detailContent($scope.datas.goods);
						}
					}

				}

			}
			$scope.goodsModal.hide();
		};
		//当我们用到模型时，清除它！
		$scope.$on('$destroy', function() {
			if($scope.regionModal)
				$scope.regionModal.remove();
			if($scope.goodsModal)
				$scope.goodsModal.remove();
		});
		//点击其他区域关闭弹出窗
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {
			if(event.target.nodeName === 'ION-CONTENT') {
				if($scope.goodsModal) {
					$scope.goodsModal.hide();
				}
				if($scope.specModal) {
					$scope.specModal.hide();
				}
			}
		});

		$scope.do_agentSearch = function() {
			if($scope.input.province != null){
				$scope.input.region = $scope.input.province.id;
			}
			if($scope.input.city != null){
				$scope.input.region += ','+$scope.input.city.id;
			}
			if($scope.input.district != null){
				$scope.input.region += ','+$scope.input.district.id;
			}
			if($scope.input.key != null || $scope.input.region != null) {
				Group.agenter_getlist($scope.input.key, $scope.input.region, function(data) {
					$scope.agenter_list = data;
					$scope.input.agent_count = $scope.agenter_list.page[0].count;
				})
			}

		}
		$scope.agenter_checked = function(store_id) {
			for(var i = 0; i < $scope.agenter_list.store.length; i++) {
				if($scope.agenter_list.store[i].store_id == store_id) {
					if($scope.agenter_list.store[i].checked == true)
						$scope.agenter_list.store[i].checked = false;
					else
						$scope.agenter_list.store[i].checked = true;
				}
			}
		}
		$scope.do_goodsSearch = function() {
			if($scope.input.cat1 != null) {
				$scope.input.cat_type = $scope.input.cat1;
			}
			if($scope.input.cat2 != null) {
				$scope.input.cat_type += ',' + $scope.input.cat2;
			}
			if($scope.input.cat3 != null) {
				$scope.input.cat_type += ',' + $scope.input.cat3;
			}
			if($scope.input.cat_type != null || $scope.input.name != null || $scope.input.brand_id != null) {
				Group.goods_list($scope.input.cat_type, $scope.input.name, $scope.input.brand_id, function(data) {
					$scope.goods_data = data;
					$scope.input.goods_count = $scope.goods_data.page[0].count;
				})
			}
		}
		$scope.goods_checked = function(goods_id) {
			for(var i = 0; i < $scope.goods_data.goods.length; i++) {
				if($scope.goods_data.goods[i].goods_id == goods_id) {
					if($scope.goods_data.goods[i].checked == true) {
						$scope.goods_data.goods[i].checked = false;
					} else
						$scope.goods_data.goods[i].checked = true;

				}
			}

		}
	}

	controller.$inject = ['$scope', 'Group', '$window', '$timeout', '$ionicSlideBoxDelegate', '$ionicPopover', 'GoPage', 'GoDialog', '$stateParams','GoAddress','$ionicModal','GoPicture'];

	return app.registerController("groupctrl",controller);
});